<?php

if (!defined('IN_XLP')) {
        exit('Access Denied!');
}

/**
 * Description of eventAction
 *
 * @author xlp
 */
//设置后台模板目录
define('GROUP_TPL_DIR', 'cevent');

class eventAction extends commonAction {

        function __construct() {
                parent::__construct();
                parent::_checkLogin();
                $this->eventStatus = array(1 => '正常', 0 => '<span class="red">受限</span>');
                $this->eventType = array(0 => '全部', 1 => '优惠劵', 2 => '大转盘', 3 => '刮刮卡', 4 => '微信互动', 6 => '翻牌抽奖', 7 => '疯狂猜图', 8 => '老虎机', 9 => '砸金蛋', 11 => '投票', 10 => '拼图达人', 13 => '抢红包', 14 => '微信点灯', 15 => '微信种树', 16 => '摇一摇', 17 => '小管家抵价券', 18 => '水果机抽奖', 19 => '微信现金红包');
                $this->picSize = array(
                  1 => array('top' => '', 'bottom' => ''),
                  2 => array('top' => '300px*300px', 'bottom' => '300px*306px'),
                  3 => array('top' => '150px*39px', 'bottom' => '273px*238px'),
                  4 => array('top' => '', 'bottom' => ''),
                  5 => array('top' => '', 'bottom' => ''),
                  6 => array('top' => '90px*90px', 'bottom' => ''),
                  7 => array('top' => '300px*200px', 'bottom' => ''),
                  8 => array('top' => '300px*560px', 'bottom' => ''),
                  9 => array('top' => '93px*110px', 'bottom' => '93px*110px','share'=>'120px*120px'),
                  10 => array('top' => '100px*100px', 'bottom' => ''),
                  11 => array('top' => '400px*650px', 'bottom' => '266px*148px'),
                  12 => array('top' => '', 'bottom' => ''),
                  13 => array('top' => '', 'bottom' => ''),
                  14 => array('top' => '', 'bottom' => ''),
                  15 => array('top' => '', 'bottom' => ''),
                  16 => array('top' => '', 'bottom' => '', 'shake_pic' => '340px*340px'),
                  17 => array('top' => '', 'bottom' => ''),
                  18 => array('top' => '', 'bottom' => ''),
                  19 => array('top' => '', 'bottom' => ''),
                );
                $this->eventList = $this->eventType;
                unset($this->eventList[0]);
                $this->assign(array('status' => $this->eventStatus, 'type' => $this->eventType, 'eventList' => $this->eventList, 'picSize' => $this->picSize));
        }

        function index() {
                $p = $this->_getid('p');
                $t = $this->_getid('t', 0);
                $where = array('aid' => admin::$adminInfo['aid']);
                if ($t) {
                        $where['type'] = $t;
                }
                $rs = M('event')->where($where)->page($p)->order('id DESC')->findAll();
                $this->assign(array('rs' => $rs, 'p' => $p, 'total' => M('event')->getTotal(), 't' => $t));
                $this->display();
        }

        function prize() {
//        M('prize')->setShowError();
                $id = $this->_getid('id', 0);
                $this->status = array(0 => '未处理', 1 => '<span class="red">已处理</span>');
                $p = $this->_getid('p', 1);
                $limit = (($p - 1) * 20) . ',20';
                $where = 'a.aid=' . admin::$adminInfo['aid'] . " AND a.prize<>''";
                if ($id) {
                        $where.=' AND a.id=' . $id;
                }
                $sql = 'SELECT a.*,b.title,c.nickname,c.phone FROM __TABLE__ AS a LEFT JOIN __PRE__event AS b ON a.id=b.id AND a.aid=b.aid '
                        . 'LEFT JOIN __PRE__user AS c ON a.uid=c.uid AND a.aid=c.aid '
                        . 'WHERE ' . $where . ' order by pid DESC LIMIT ' . $limit;
                $rs = M('prize')->query($sql);
                $total = M('prize')->where(str_replace('a.', '', $where))->count('id');
                //统计各奖品中奖人数
                $prizeTotal = array();
                if ($id) {
                        $sql = 'SELECT prize,count(*) AS total FROM __TABLE__ where ' . str_replace('a.', '', $where) . ' GROUP BY prize ORDER BY total ASC';
//        echo $sql;
                        $prizeTotal = M('prize')->query($sql);
                }
                $this->assign(array('rs' => $rs, 'p' => $p, 'total' => $total, 'status' => $this->status, 'prizeTotal' => $prizeTotal));
                //开始日期
                $stime = date('Y-m-d', strtotime(' - 7 days'));
                //结束日期
                $etime = date('Y-m-d');
                $this->assign(array('stime' => $stime, 'etime' => $etime, 'id' => $id));
                $this->display();
        }

        function geturl() {
                $id = $this->_getid('id');
                if (!$id) {
                        showError('链接不存在');
                }
                $this->assign(array('id' => $id));
                $this->display();
        }

        //拼图游戏用户列表
        function puzzle() {
                $id = $this->_getid('id', 0);
                $order = $this->_getid('t', 0);
                $p = $this->_getid('p', 1);
                $this->order = array(0 => 'infotime', 1 => 'times', 2 => 'steps');
                $limit = (($p - 1) * 20) . ',20';
                $where = 'a.aid=' . admin::$adminInfo['aid'];
                $orderBy = ' a.infotime asc';
                if ($id) {
                        $where.=' AND a.gid=' . $id;
                }
                if (isset($this->order[$order])) {
                        $orderBy = ' a.' . $this->order[$order] . ' ASC ';
                        if ($order) {
                                $orderBy .= ',a.infotime ASC ';
                        }
                }
                if ($order == 5) {
                        $field = ' steps ASC,';
                } elseif ($order == 4) {
                        $field = ' times ASC,';
                } else {
                        $field = '';
                }
//        M('game_puzzle')->setShowError();
                if ($order < 3) {
                        $sql = 'SELECT SQL_CALC_FOUND_ROWS a.id,a.uid,a.times,a.steps,a.infotime,b.title,c.username,c.phone '
                                . 'FROM __TABLE__ AS a LEFT JOIN __PRE__event AS b ON a.gid=b.id AND a.aid=b.aid '
                                . 'LEFT JOIN __PRE__user AS c ON a.uid=c.uid AND a.aid=c.aid '
                                . 'WHERE ' . $where . ' order by ' . $orderBy . ' LIMIT ' . $limit;
                } else {
                        $sql = 'SELECT SQL_CALC_FOUND_ROWS a.id,a.uid,a.times,a.steps,a.infotime,b.username,b.phone,c.title '
                                . 'FROM (SELECT * FROM __TABLE__ ORDER BY ' . $field . 'infotime ASC ) AS a '
                                . 'LEFT JOIN __PRE__user AS b ON a.uid=b.uid AND a.aid=b.aid '
                                . 'LEFT JOIN __PRE__event AS c ON a.gid=c.id '
                                . 'WHERE ' . 'a.gid=' . $id . ' GROUP BY a.uid order by ' . $field . 'infotime ASC LIMIT ' . $limit;
                }
                $rs = M('game_puzzle')->query($sql);
                $total = M('game_puzzle')->query('SELECT FOUND_ROWS() AS total', true);
                $total = isset($total['total']) ? $total['total'] : 0;
                $this->assign(array('rs' => $rs, 'p' => $p, 'total' => $total, 'id' => $id));
                $this->display('prize_puzzle');
        }

        function puzzleoutput() {
                $stime = $this->_get('stime', 0);
                $etime = $this->_get('etime', 0);
                $id = $this->_get('id', 0);
                $where = '';
                if ($id) {
                        $where = ' AND a.id=' . $id;
                }
                if ($stime && $etime) {
                        $where.=' AND a.infotime>=' . strtotime($stime) . ' AND a.infotime<' . (strtotime($etime) + 86400);
                }
                $sql = 'SELECT a.uid,a.code,a.prize,a.infotime,a.status,b.nickname,b.phone FROM __TABLE__ AS a LEFT JOIN   
            __PRE__user AS b ON a.uid=b.uid AND a.aid=b.aid WHERE 
                ' . 'a.aid=' . admin::$adminInfo['aid'] . ' AND a.prize<>\'\'  ' . $where . ' order by a.pid DESC';
                $rs = M('prize')->query($sql);
                $status = array(0 => '未处理', 1 => '已处理');
                $list = array();
                foreach ($rs as $val) {
                        $list[] = array(
                          $val['uid'],
                          $val['nickname'],
                          $val['phone'],
                          "\t" . $val['code'],
                          $val['prize'],
                          "\t" . outTime($val['infotime']),
                          $status[$val['status']]
                        );
                }
                load('csv');
                $myCsv = new csv();
                $myCsv->write(array('UID', '姓名', '手机号', '地址', '中奖码', '类型', '时间', '状态'), $list, 'prize_' . date('y-m-d'));
        }

        function output() {
                $stime = $this->_get('stime', 0);
                $etime = $this->_get('etime', 0);
                if (!$stime || !$etime) {
                        showError('时间段错误');
                }
                $id = $this->_get('id', 0);
                $where = '';
                if ($id) {
                        $where = ' AND a.id=' . $id;
                }
                $sql = 'SELECT a.uid,a.code,a.prize,a.infotime,a.status,b.nickname,b.phone FROM __TABLE__ AS a '
                        . 'LEFT JOIN __PRE__user AS b ON a.uid=b.uid AND a.aid=b.aid '
                        . 'WHERE ' . 'a.aid=' . admin::$adminInfo['aid'] . ' AND a.prize<>\'\' AND a.infotime>=' . strtotime($stime) . ' '
                        . 'AND a.infotime<' . (strtotime($etime) + 86400) . $where . ' order by a.pid DESC';
                $rs = M('prize')->query($sql);
                $status = array(0 => '未处理', 1 => '已处理');
                $list = array();
                foreach ($rs as $val) {
                        $list[] = array(
                          $val['uid'],
                          $val['nickname'],
                          $val['phone'],
                          "\t" . $val['code'],
                          $val['prize'],
                          "\t" . outTime($val['infotime']),
                          $status[$val['status']]
                        );
                }
                load('csv');
                $myCsv = new csv();
                $myCsv->write(array('UID', '姓名', '手机号', '地址', '中奖码', '类型', '时间', '状态'), $list, "prize_" . date('y-m-d'));
        }

        //处理中奖者状态
        function deal() {
                $id = $this->_getid('id');
                $act = $this->_get('act');
                if (!$id || !in_array($act, array('ok', 'invalid'))) {
                        showError('参数有误');
                }
                $status = 0;
                if ($act == 'ok') {
                        $status = 1;
                } else {
                        $status = 0;
                }
                M('prize')->update(array('status' => $status), array('pid' => $id));
                $this->JsonReturn('ok', null, 1);
        }

//添加活动
        function add() {
                $this->display();
        }

//编辑活动
        function edit() {
                $id = $this->_getid('id');
                if (!$id) {
                        showError('活动不存在');
                }
                $sql = 'SELECT a.*,b.itemid,b.picurl,b.content,b.setting,b.skin_path FROM __TABLE__ AS a '
                        . 'LEFT JOIN __PRE__event_item AS b ON a.id=b.id AND a.aid=b.aid '
                        . 'WHERE a.aid=' . admin::$adminInfo['aid'] . ' AND a.id=' . $id;
                $rs = M('event')->query($sql, true);
                if (!$rs) {
                        showError('用户不存在');
                }
                if ($rs['setting']) {
                        $rs['setting'] = json_decode($rs['setting'], true);
                }
//        print_r($rs);exit;
                $this->assign(array('rs' => $rs, 'tpls' => $this->getPluginTpl($rs['type'])));
                if ($rs['type'] == 4 || $rs['type'] == 5) {//互动推广
                        $this->display('edit_interact');
                } elseif ($rs['type'] == 6) {
                        $this->display('edit_flop');
                } elseif ($rs['type'] == 7 || $rs['type'] == 12) {
                        $this->display('edit_guess');
                } elseif ($rs['type'] == 8) {
                        $this->display('edit_slot');
                } elseif ($rs['type'] == 10) {
                        $this->display('edit_puzzle');
                } elseif ($rs['type'] == 11) {
                        $this->display('edit_vote');
                } elseif ($rs['type'] == 16) {
                        $this->display('edit_shake');
                } elseif ($rs['type'] == 17) {
                        $this->display('edit_ticket');
                } else if ($rs['type'] == 18) {
                        $this->display('edit_fruit');
                } else {
                        $this->display();
                }
        }

//添加活动详情
        function setting() {
                $id = $this->_getid('id');
                if (!$id) {
                        showError('参数丢失');
                }
                $rs = M('event')->where(array('aid' => admin::$adminInfo['aid'], 'id' => $id))->find();
                if (!$rs) {
                        showError('活动不存在');
                }
                $this->assign(array('rs' => $rs, 'tpls' => $this->getPluginTpl($rs['type'])));
                if ($rs['type'] == 4 || $rs['type'] == 5) {//互动推广
                        $this->display('interact');
                } elseif ($rs['type'] == 6) {
                        $this->display('flop');
                } elseif ($rs['type'] == 7 || $rs['type'] == 12) {
                        $this->display('guess');
                } elseif ($rs['type'] == 8) {
                        $this->display('slot');
                } elseif ($rs['type'] == 10) {
                        $this->display('puzzle');
                } elseif ($rs['type'] == 11) {
//            默认投票参数
                        $rs['vote'] = array(
                          array(
                            'title' => '',
                            'type' => 1,
                            'total' => 1,
                            'items' => array(
                              array(
                                's' => '',
                                't' => 0,
                                'p' => ''
                              )
                            )
                          )
                        );
                        $this->assign(array(
                          'rs' => $rs
                        ));
                        $this->display('vote');
                } elseif ($rs['type'] == 17) {
                        $this->display('ticket');
                } else if ($rs['type'] == 18) {
                        $this->display('fruit');
                } else {
                        $this->display();
                }
        }

//保存活动
        function post() {
                $field = array('title', 'type', 'stime', 'etime', 'keyword', 'recontent');
                $objData = array();
                foreach ($field as $val) {
                        $objData[$val] = $this->_post($val);
                        if (empty($objData[$val])) {
                                $this->JsonReturn('error', 'empty', 0);
                        }
                }
                //修正参数
                if (D('reply')->checkReplyIsExists($objData['keyword'])) {
                        $this->JsonReturn('error', 'keyword', 0);
                } else {
                        $objData['aid'] = admin::$adminInfo['aid'];
                        $objData['times'] = $this->_postid('times', 0);
                        $objData['everyday'] = $this->_postid('everyday', 0);
                        $objData['shares'] = $this->_postid('shares', 0);
                        $objData['hasbind'] = $this->_postid('hasbind', 0);
                        $objData['share_title'] = $this->_post('share_title');
                        $objData['share_content'] = $this->_post('share_content');
                        $objData['stime'] = inTime($objData['stime']);
                        $objData['etime'] = inTime($objData['etime']);
//            print_r($objData);exit;
                        $fid = M('event')->insert($objData);
                        F('replyCache_u' . admin::$adminInfo['aid'], null); //清理自定义回复缓存文件
                        if ($fid) {
                                D('reply')->insert(array('aid' => $objData['aid'], 'match_type' => 0, 'keyword' => $objData['keyword'], 'reply_type' => 3, 'content' => '', 'fid' => $fid));
                                $this->JsonReturn('ok', U('event/setting', array('id' => $fid)), 1);
                        } else {
                                $this->JsonReturn('error');
                        }
                }
        }

//保存详情
        function saveset() {
//        print_r($_POST);
//        exit;
                $picurl = $this->_post('picurl', '');
                $content = $this->_post('content');
                $id = $this->_postid('id');
                $item = $this->_post('item');
                $inter = $this->_post('inter');
                $puzzle = $this->_post('puzzle');
                $custom = $this->_post('custom');
                $vote = $this->_post('vote');
                $skin_path = $this->_post('skin_path');
                $objData = array();
                $itemInter = array();
                $countTotal = 0;
                $totalInter = 0;
//        print_r($item);
              //抵价券添加
                $ticket = $this->_post('ticket','');
                $arr = array();
                if ($ticket) {
                    $count = count($ticket['ticket_id']);
                        for ($i = 0; $i < $count; $i+=1) {
                                $ticket_id=$ticket['ticket_id'][$i]?intval($ticket['ticket_id'][$i]):0;
                                $arr[$ticket_id]=array(
                                  'ticket_id'=>$ticket_id,
                                  'ticket_money'=>$ticket['ticket_money'][$i]?priceFormat($ticket['ticket_money'][$i]):0,
                                  'ticket_amount'=>$ticket['ticket_amount'][$i]?priceFormat($ticket['ticket_amount'][$i]):0,
                                  'ticket_day'=>$ticket['ticket_day'][$i]?intval($ticket['ticket_day'][$i]):0,
                                );
                        }
                }
                //抵价券添加
                if ($item) {
                        $count = count($item['name']);
                        for ($i = 0; $i < $count; $i+=1) {
                                if ($item['name'][$i]) {
                                        $total = intval($item['count'][$i]);
                                        $countTotal+=$total;
                                        $objData[] = array(
                                          'name' => $item['name'][$i],
                                          'gift' => $item['gift'][$i],
                                          'credit' => isset($item['credit'][$i]) ? intval($item['credit'][$i]) : 0,
                                           'ticket_id' => isset($item['ticket_id'][$i])?intval($item['ticket_id'][$i]):0, //抵价券ID
                                          'count' => intval($total),
                                          'rate' => $item['rate'][$i] ? $item['rate'][$i] : 0,
                                          'pic' => isset($item['pic'][$i]) ? $item['pic'][$i] : '',
                                          'content' => $item['content'][$i]
                                        );
                                }
                        }
                        $totalInter = $inter && isset($inter['name']) ? count($inter['name']) : 0;
                        for ($i = 0; $i < $totalInter; $i+=1) {
                                if ($inter['name']) {
                                        $itemInter[] = array(
                                          'name' => $inter['name'][$i],
                                          'content' => $inter['content'][$i],
                                          'pic' => isset($inter['pic'][$i]) ? $inter['pic'][$i] : '',
                                          'credit_add' => isset($inter['credit_add'][$i]) && checkNum($inter['credit_add'][$i]) ? intval($inter['credit_add'][$i]) : 0,
                                          'credit_del' => isset($inter['credit_del'][$i]) && checkNum($inter['credit_del'][$i]) ? intval($inter['credit_del'][$i]) : 0
                                        );
                                }
                        }
                        M('event_item')->insert(array('id' => $id, 'aid' => admin::$adminInfo['aid'], 'skin_path' => $skin_path, 'picurl' => $picurl, 'content' => $content, 'setting' => json_encode(array('total' => $countTotal, 'item' => $objData,  'ticket' => $arr,'total_inter' => $totalInter, 'item_inter' => $itemInter, 'custom' => $custom)))); //抵价券添加
                        $this->JsonReturn('ok', null, 1);
                } elseif ($puzzle) {
//            print_r($puzzle);exit;
                        M('event_item')->insert(array('id' => $id, 'aid' => admin::$adminInfo['aid'], 'skin_path' => $skin_path, 'picurl' => $picurl, 'content' => $content, 'setting' => json_encode(array('puzzle' => $puzzle))));
                        $this->JsonReturn('ok', null, 1);
                } elseif ($vote) {
//            print_r($vote);
//            exit;
                        $objData = array();
                        $maxTotal = 0;
                        $isImg = 0; //是否图片投票
                        foreach ($vote as $val) {
                                $total = count($val['s']);
                                $items = array();
                                $count = 0;
                                for ($i = 0; $i < $total; $i+=1) {
                                        $items[$i] = array(
                                          's' => isHave($val['s'][$i]) ? $val['s'][$i] : '', //选项
                                          't' => isHave($val['t'][$i]) ? intval($val['t'][$i]) : 0, //票数
                                          'p' => isHave($val['p'][$i]) ? $val['p'][$i] : '', //图片
                                        );
                                        if (!$isImg && $items[$i]['p']) {
                                                $isImg = 1;
                                        }
                                        $count+=$items[$i]['t'];
                                }
                                $objData[] = array(
                                  'title' => $val['title'], //标题
                                  'total' => $count, //总票数
                                  'type' => $val['type'], //类型 1：单选 2：多选
                                  'max' => intval($val['max']), //多选时最多可选值
                                  'items' => $items
                                );
                                $maxTotal = $count > $maxTotal ? $count : $maxTotal;
                        }
//            print_r($objData);
//            exit;
                        M('event_item')->insert(array(
                          'id' => $id,
                          'aid' => admin::$adminInfo['aid'],
                          'skin_path' => $skin_path,
                          'picurl' => $picurl,
                          'content' => $content,
                          'setting' => json_encode(array('vote' => $objData, 'custom' => $custom, 'total' => $maxTotal, 'imgVote' => $isImg))));
                        $this->JsonReturn('ok', null, 1);
                } else {
                        $this->JsonReturn('error', 'set', 0);
                }
        }

//保存编辑
        function save() {
                $id = $this->_postid('id');
                if (!$id) {
                        $this->JsonReturn('error');
                }
                $sql = 'SELECT a.*,b.itemid,b.picurl,b.content,b.setting FROM __TABLE__ AS a '
                        . 'LEFT JOIN __PRE__event_item AS b ON a.id=b.id AND a.aid=b.aid '
                        . 'WHERE a.aid=' . admin::$adminInfo['aid'] . ' AND a.id=' . $id;
                $rs = M('event')->query($sql, true);
                if (!$rs) {
                        showError('用户不存在');
                }
//        print_r($rs);exit;
//保存活动
                $field = array('title', 'type', 'stime', 'etime', 'keyword', 'recontent');
                $objData = array();
                foreach ($field as $val) {
                        $objData[$val] = $this->_post($val);
                        if (empty($objData[$val])) {
                                $this->JsonReturn('error', 'empty', 0);
                        }
                }
                //修正参数
                $checkKeyWord = D('reply')->checkReplyIsExists($objData['keyword']);
                if ($checkKeyWord && ($checkKeyWord['reply_type'] != 3 || $checkKeyWord['fid'] != $id)) {
                        $this->JsonReturn('error', 'keyword', 0);
                } else {
                        $objData['times'] = $this->_postid('times', 0);
                        $objData['everyday'] = $this->_postid('everyday', 0);
                        $objData['shares'] = $this->_postid('shares', 0);
                        $objData['hasbind'] = $this->_postid('hasbind', 0);
                        $objData['share_title'] = $this->_post('share_title');
                        $objData['share_content'] = $this->_post('share_content');
                        $objData['stime'] = inTime($objData['stime']);
                        $objData['etime'] = inTime($objData['etime']);
//            print_r($objData);exit;
                        M('event')->update($objData, array('aid' => admin::$adminInfo['aid'], 'id' => $id));
                        if (!$checkKeyWord) {//新的关键字
                                //删除旧的关键字，再插入新的
                                D('reply')->delete(array('reply_type' => 3, 'aid' => admin::$adminInfo['aid'], 'fid' => $id));
                                D('reply')->insert(array('aid' => admin::$adminInfo['aid'], 'match_type' => 0, 'keyword' => $objData['keyword'], 'reply_type' => 3, 'content' => '', 'fid' => $id));
                        } else {
                                $oldKeyWord = D('reply')->where(array('reply_type' => 3, 'aid' => admin::$adminInfo['aid'], 'fid' => $id))->find();
                                if (isset($oldKeyWord['keyword']) && $oldKeyWord['keyword'] != $objData['keyword']) {
                                        D('reply')->update(array('keyword' => $objData['keyword']), array('reply_type' => 3, 'aid' => admin::$adminInfo['aid'], 'fid' => $id));
                                }
                        }
                }
//保存详情
                $picurl = $this->_post('picurl', '');
                $content = $this->_post('content');
                $item = $this->_post('item');
                $t = $objData['type'];
                $inter = $this->_post('inter');
                $puzzle = $this->_post('puzzle');
                $custom = $this->_post('custom');
                $vote = $this->_post('vote');
                $skin_path = $this->_post('skin_path');
                $itemInter = array();
                $totalInter = 0;
                $objData = array();
                $countTotal = 0; //总中奖人数
                //抵价券添加
                $ticket = $this->_post('ticket','');
                $arr = array();
                if ($ticket) {
                    $count = count($ticket['ticket_id']);
                        for ($i = 0; $i < $count; $i+=1) {
                                $ticket_id=$ticket['ticket_id'][$i]?intval($ticket['ticket_id'][$i]):0;
                                $arr[$ticket_id]=array(
                                  'ticket_id'=>$ticket_id,
                                  'ticket_money'=>$ticket['ticket_money'][$i]?priceFormat($ticket['ticket_money'][$i]):0,
                                  'ticket_amount'=>$ticket['ticket_amount'][$i]?priceFormat($ticket['ticket_amount'][$i]):0,
                                  'ticket_day'=>$ticket['ticket_day'][$i]?intval($ticket['ticket_day'][$i]):0,
                                );
                        }
                }
                //抵价券添加
                if ($item) {
                        $count = count($item['name']);
                        for ($i = 0; $i < $count; $i+=1) {
                                if ($item['name'][$i]) {
                                        $total = intval($item['count'][$i]);
                                        $countTotal+=$total;
                                        $objData[] = array(
                                          'name' => $item['name'][$i],
                                          'gift' => $item['gift'][$i],
                                          'credit' => isset($item['credit'][$i]) ? intval($item['credit'][$i]) : 0,
                                          'ticket_id' => isset($item['ticket_id'][$i])?intval($item['ticket_id'][$i]):0, //抵价券ID
                                          'count' => intval($total),
                                          'rate' => $item['rate'][$i] ? $item['rate'][$i] : 0,
                                          'pic' => isset($item['pic'][$i]) ? $item['pic'][$i] : '',
                                          'content' => $item['content'][$i]
                                        );
                                }
                        }
                        $totalInter = $inter && isset($inter['name']) ? count($inter['name']) : 0;
                        for ($i = 0; $i < $totalInter; $i+=1) {
                                if ($inter['name']) {
                                        $itemInter[] = array(
                                          'name' => $inter['name'][$i],
                                          'content' => $inter['content'][$i],
                                          'pic' => isset($inter['pic'][$i]) ? $inter['pic'][$i] : '',
                                          'credit_add' => isset($inter['credit_add'][$i]) && checkNum($inter['credit_add'][$i]) ? intval($inter['credit_add'][$i]) : 0,
                                          'credit_del' => isset($inter['credit_add'][$i]) && checkNum($inter['credit_add'][$i]) ? intval($inter['credit_del'][$i]) : 0
                                        );
                                }
                        }
                        $objData = json_encode(array('total' => $countTotal, 'item' => $objData, 'ticket' => $arr, 'total_inter' => $totalInter, 'item_inter' => $itemInter, 'custom' => $custom)); //抵价券添加
                } elseif ($puzzle) {
                        $objData = json_encode(array('puzzle' => $puzzle));
                } elseif ($vote) {
//            print_r($vote);
//            exit;
                        $voteData = array();
                        $maxTotal = 0;
                        $isImg = 0; //是否图片投票
                        foreach ($vote as $val) {
                                $total = count($val['s']);
                                $items = array();
                                $count = 0;
                                for ($i = 0; $i < $total; $i+=1) {
                                        $items[$i] = array(
                                          's' => isHave($val['s'][$i]) ? $val['s'][$i] : '', //选项
                                          't' => isHave($val['t'][$i]) ? intval($val['t'][$i]) : 0, //票数
                                          'p' => isHave($val['p'][$i]) ? $val['p'][$i] : '', //图片
                                        );
                                        if (!$isImg && $items[$i]['p']) {
                                                $isImg = 1;
                                        }
                                        $count+=$items[$i]['t'];
                                }
                                $voteData[] = array(
                                  'title' => $val['title'], //标题
                                  'total' => $count, //总票数
                                  'type' => $val['type'], //类型 1：单选 2：多选
                                  'max' => intval($val['max']), //多选时最多可选值
                                  'items' => $items
                                );
                                $maxTotal = $count > $maxTotal ? $count : $maxTotal;
                        }
//            print_r($objData);
//            exit;
                        $objData = json_encode(array('vote' => $voteData, 'custom' => $custom, 'total' => $maxTotal, 'imgVote' => $isImg));
                } else {
                        $this->JsonReturn('error', 'set', 0);
                }
                if (!$rs['itemid']) {
                        M('event_item')->insert(array('id' => $id, 'aid' => admin::$adminInfo['aid'], 'skin_path' => $skin_path, 'picurl' => $picurl, 'content' => $content, 'setting' => $objData));
                } else {
                        M('event_item')->update(array('skin_path' => $skin_path, 'picurl' => $picurl, 'content' => $content, 'setting' => $objData), array('itemid' => $rs['itemid'], 'id' => $id, 'aid' => admin::$adminInfo['aid']));
                }
                F('replyCache_u' . admin::$adminInfo['aid'], null); //清理自定义回复缓存文件
                $this->JsonReturn('ok', U('event/index', array('t' => $t)), 1);
        }

        function delete() {
                $id = $this->_postid('id');
                if (!$id) {
                        showError('参数丢失');
                }
                M('event')->delete(array('id' => $id, 'aid' => admin::$adminInfo['aid']));
                M('event_item')->delete(array('id' => $id, 'aid' => admin::$adminInfo['aid']));
                D('reply')->delete(array('fid' => $id, 'aid' => admin::$adminInfo['aid'], 'reply_type' => 3));
                M('prize')->delete(array('id' => $id, 'aid' => admin::$adminInfo['aid']));
                F('replyCache_u' . admin::$adminInfo['aid'], null); //清理自定义回复缓存文件
                $this->JsonReturn('ok', null, 1);
        }

        //获取全部的插件模板
        private function getPluginTpl($type = 1) {
                $list = glob(ROOT . 'plugins/prize/*/config.php', GLOB_NOSORT);
                $arr = array('默认模板' => array('path' => 'default'));
                foreach ($list as $val) {
                        $data = @include $val;
                        if (isHave($data['aid']) && $data['aid'] == admin::$adminInfo['aid'] && isHave($data['type']) && $data['type'] == $type && checkPath($data['path'])) {
                                $arr[$data['name']] = $data;
                        }
                }
                return $arr;
        }

}

function getStatus($stime, $etime) {
        if ($stime <= TIME) {
                if ($etime < TIME) {
                        return '<span class="grey">已结束</span>';
                } elseif ($etime >= TIME) {
                        return '<span class="red">正在进行</span>';
                }
        } else {
                return '即将开始';
        }
}

/*
 * 菜单配置文件
 */

function getMenuData() {
        return array(
          array('title' => '活动管理', 'url' => '', 'short' => 'event', 'item' =>
            array(
              array(
                'title' => '添加活动',
                'url' => U('event/add'),
                'short' => 'event-add'
              ),
              array(
                'title' => '优惠劵活动',
                'url' => U('event/index', array('t' => 1)),
                'short' => 'event-index-1'
              ),
              array(
                'title' => '大转盘抽奖',
                'url' => U('event/index', array('t' => 2)),
                'short' => 'event-index-2'
              ),
              array(
                'title' => '刮刮卡活动',
                'url' => U('event/index', array('t' => 3)),
                'short' => 'event-index-3'
              ),
              array(
                'title' => '互动问答',
                'url' => U('event/index', array('t' => 4)),
                'short' => 'event-index-4'
              ),
              array(
                'title' => '翻牌抽奖',
                'url' => U('event/index', array('t' => 6)),
                'short' => 'event-index-6'
              ),
              array(
                'title' => '疯狂猜图',
                'url' => U('event/index', array('t' => 7)),
                'short' => 'event-index-7'
              ),
              array(
                'title' => '老虎机',
                'url' => U('event/index', array('t' => 8)),
                'short' => 'event-index-8'
              ),
              array(
                'title' => '砸金蛋',
                'url' => U('event/index', array('t' => 9)),
                'short' => 'event-index-9'
              ),
              array(
                'title' => '拼图达人',
                'url' => U('event/index', array('t' => 10)),
                'short' => 'event-index-10'
              ),
              array(
                'title' => '网络投票',
                'url' => U('event/index', array('t' => 11)),
                'short' => 'event-index-11'
              ),
              array(
                'title' => '抢红包',
                'url' => U('event/index', array('t' => 13)),
                'short' => 'event-index-13'
              ),
              array(
                'title' => '微信点灯',
                'url' => U('event/index', array('t' => 14)),
                'short' => 'event-index-14',
              ),
              array(
                'title' => '微信种树',
                'url' => U('event/index', array('t' => 15)),
                'short' => 'event-index-15',
              ),
              array(
                'title' => '摇一摇',
                'url' => U('event/index', array('t' => 16)),
                'short' => 'event-index-16',
              ),
              array(
                'title' => '小管家抵价券',
                'url' => U('event/index', array('t' => 17)),
                'short' => 'event-index-17',
              ),
              array(
                'title' => '水果机抽奖',
                'url' => U('event/index', array('t' => 18)),
                'short' => 'event-index-18',
              ),
              array(
                'title' => '微信现金红包',
                'url' => U('event/index', array('t' => 19)),
                'short' => 'event-index-19',
              ),
              array(
                'title' => '活动列表',
                'url' => U('event/index'),
                'short' => 'event-index-0'
              ),
              array(
                'title' => '中奖名单',
                'url' => U('event/prize'),
                'short' => 'event-prize'
              )
            )
          )
        );
}
